package com.onemileshop.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.onemileshop.controls.AdpCstLinearLayout;
import com.onemileshop.controls.CstLinearLayout;
import com.onemileshop.entiy.Session;
import com.onemileshop.entiy.model.EOrder;
import com.onemileshop.entiy.model.EOrderDetails;
import com.onemileshop.utils.UtiDialogsFactory;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class UIOrderDetails extends Activity
{


   private CstLinearLayout layListView;

//   private TextView codeTextView;
//   private TextView sNumTextView;
   private TextView stateTextView;
   private TextView dateTextView;
   private TextView money1TextView;
   private TextView customerTextView;
   private TextView addressTextView;
   private CheckBox chbHasReturn;
   private Button btnOK;
   private TableRow row;
   
   private TextView mobileTextView;
   private LinearLayout layContact;
   private Button btnCall;
   
   // private TextView paytypeTextView;
   // private TextView money2TextView;


  private void InitControls()
   {
	  layListView = (CstLinearLayout) findViewById(R.id.layListView_ui_orderdetails);

//	  codeTextView = (TextView) findViewById(R.id.txtOrderCode_ui_orderdetails);
//	  sNumTextView = (TextView) findViewById(R.id.txtSNumber_ui_orderdetails);
	  stateTextView = (TextView) findViewById(R.id.txtOrderState_ui_orderdetails);
	  dateTextView = (TextView) findViewById(R.id.txtOrderDate_ui_orderdetails);
	  money1TextView = (TextView) findViewById(R.id.txtAmount_ui_orderdetails);
	  customerTextView = (TextView) findViewById(R.id.txtCustomer_ui_orderdetails);
	  addressTextView = (TextView) findViewById(R.id.txtAddress_ui_orderdetails); 
	  mobileTextView=(TextView)findViewById(R.id.txtMobile_ui_orderdetails); 
	  btnCall=(Button)findViewById(R.id.btnCall_ui_orderdetails);
//	  chbHasReturn = (CheckBox) findViewById(R.id.ckbHasReturn_ui_orderdetails);
//	  btnOK = (Button) findViewById(R.id.btnOK_ui_orderdetails);
//	  row = (TableRow) findViewById(R.id.row_orderdetails);
//	  
	 
//	  layContact=(LinearLayout)findViewById(R.id.layContact_ui_orderdetails);
	 
//	  
	  
	  
	  // paytypeTextView=(TextView)findViewById(R.id.txtPayType_Act_orderdetails);
	  // money2TextView=(TextView)findViewById(R.id.txtOrderAmount2_Act_orderdetails);
   }

  private void InitControlsListener()
   {
//	  btnOK.setOnClickListener(new OnClickListener()
//	  {
//		 @Override
//		 public void onClick(View v)
//		 {
//			DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener()
//			{
//			   @Override
//			   public void onClick(DialogInterface dialog, int which)
//			   {
//				  UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UIOrderDetails.this,"订单确认中。。。");
//				  
//				  List<ModUrlParam> list = new ArrayList<ModUrlParam>();
//				  list.add(new ModUrlParam(UrlParams.DeliveOrder.frequencysiteid, modOrder.getFrequencySiteId()));
//				  if (chbHasReturn.isChecked())
//				  {
//					list.add(new ModUrlParam(UrlParams.DeliveOrder.isreturns, String.valueOf(1)));
//				  }
//				  else
//				  {
//					list.add(new ModUrlParam(UrlParams.DeliveOrder.isreturns, String.valueOf(0)));
//				  }
//				  String url = String.format(UrlParams.DeliveOrder.URL, UtiEncrypt.GetURL(list));
//
//				  NetOrderVerify task = new NetOrderVerify(new NetOrderVerify.TaskCallBack()
//				  {
//					 @Override
//					 public void DoWorkWhenComplete(String result)
//					 {
//					   UtiDialogsFactory.getInstance().DismissLoadingDialog();
//					   try
//					   {
//						  if (Session.getInstance().IsTrafficOn())
//						  {
//						    UtiNetraffic.End(UIOrderDetails.this, "订单确认 ");
//						  }
//						  if (result == null || result.equals(""))
//						  {
//							 UtiDialogsFactory.getInstance().ShowOKAlertDialog(UIOrderDetails.this,"提示", "确认失败，请检查网络连接");
//						  }
//						  else
//						  {
//							 if(result.equals("1"))
//							 {
//								Intent intent=new Intent();
//								intent.putExtra("order", modOrder);
//                                setResult(Activity.RESULT_OK, intent);
//								finish();
//							 }
//							 else if(result.equals("0"))
//							 {
//								UtiDialogsFactory.getInstance().ShowOKAlertDialog(UIOrderDetails.this,"提示", "发送短信失败，请联系管理员");
//							 }
//							 else 
//							 {
//								UtiDialogsFactory.getInstance().ShowOKAlertDialog(UIOrderDetails.this,"提示", result);
//							 }
//						  }		
//					   }
//					   catch (Exception e)
//					   {
//						  UtiDialogsFactory.getInstance().ShowOKAlertDialog(UIOrderDetails.this,"提示", "确认失败，请检查网络连接");
//					   }
//					 }
//				  });
//				  if (Session.getInstance().IsTrafficOn())
//				  {
//					 UtiNetraffic.Start(UIOrderDetails.this);
//				  }
//				  task.execute(url);
//
//			   }
//			};
//			UtiDialogsFactory.getInstance().ShowOKCancelAlertDialog(UIOrderDetails.this,"提示", "确定提交吗？", clickListener);
//		 }
//	  });
//      
	  btnCall.setOnClickListener(new OnClickListener()
	  {
	     @Override
	     public void onClick(View v)
	     {
	         Intent phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + mobileTextView.getText().toString()));
	         startActivity(phoneIntent);
	     }
	  });
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	   
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.ui_orderdetails);

	  ArrayList<EOrderDetails> list= getIntent().getParcelableArrayListExtra("OrderDetails");
	  EOrder order=(EOrder)getIntent().getSerializableExtra("Order");
	  InitControls();
	  InitControlsListener();

	  AdpCstLinearLayout cAdapter = new AdpCstLinearLayout(UIOrderDetails.this, getAllCommodity(list), R.layout.orderdetail_list_item, 
			new String[] { "name", "accessories","attribute","num","discount","amount" }, 
			new int[] {R.id.txtName_listview_orderdetails,
		                R.id.txtAccessories_listview_orderdetails,
		                R.id.txtAttribute_listview_orderdetails,
		                R.id.txtNum_listview_orderdetails, 
		                R.id.txtDiscount_listview_orderdetails,
		                R.id.txtAmount_listview_orderdetails });
	  layListView.setAdapter(cAdapter);

//	  codeTextView.setText(modOrder.getOrderCode());
//	  sNumTextView.setText(modOrder.getSerialNumber());
	  stateTextView.setText(String.valueOf(order.getOrderType()));
	  dateTextView.setText(order.getEarliestTime().split(" ")[1]+"~"+order.getLatestTime().split(" ")[1]);
	  money1TextView.setText(order.getTotalPrice());
	  customerTextView.setText(order.getOrderName());
	  addressTextView.setText(order.getOrderAddress());
	  mobileTextView.setText(order.getOrderMobile());
   }

   /**
    * 加载所有商品
    * 
    * @return
    */
   public List<Map<String, Object>> getAllCommodity(ArrayList<EOrderDetails> mList)
   {
	  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	  Map<String, Object> map = null;
	  for (EOrderDetails cc : mList)
	  {
		 map = new HashMap<String, Object>();
		 map.put("name", cc.getCommodityName());
		 map.put("attribute", cc.getSpecificationName()+","+cc.getAttributeDetailsName());
		 map.put("accessories", cc.getCommodityAccessoriesName());
		 map.put("num", cc.getAmount());
		 map.put("discount", cc.getDiscountPrice());		 
		 map.put("amount", cc.getTotalPrice());

		 list.add(map);
	  }
	  return list;
   }

//   /**
//    * 加载所有支付方式
//    * 
//    * @return
//    */
//   public List<Map<String, Object>> getAllPayType()
//   {
////	  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
////	  Map<String, Object> map = null;
////	  for (ModPayType cc : modOrder.getPayTypeList())
////	  {
////		 map = new HashMap<String, Object>();
////		 map.put("name", cc.getPayType());
////		 map.put("amount", cc.getPayAmount());
////		 list.add(map);
////	  }
////	  return list;
//   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
	  { 
		 setResult(RESULT_CANCELED);
		 this.finish();
	  }
	  return false;
   }
}
